package com.treeroot.smsservice.service.impl;

import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.treeroot.smsservice.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @Description:
 * @Author:treeRoot
 * @Email:553503639@qq.com
 * @Date: Create in 23:43 / 2022/5/11
 */
@Service
@Transactional
public class SmsServiceImpl implements SmsService {
    /**
     * 引入 连接
     */
    @Autowired
    private SmsClient smsClient;



    /**
     * 引入配置信息
     */
    @Value("${tencent.sms.account.sms_sdk_app_id}")
    private String SMS_SDK_APP_ID;
    @Value("${tencent.sms.account.template_id}")
    private String TEMPLATE_ID;
    @Value("${tencent.sms.account.sign_name}")
    private String SIGN_NAME;

    /**
     *
     * @param code 发送的短信
     * @param phone 手机号码
     * @return
     */
    @Override
//    发送短信
    public boolean send(String code, String phone) {
//        手机集合
        String[] phoneNumbers = {"+86" + phone};
        String[] templateCode = {code};

        //实例请求，组装参数
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setSmsSdkAppid(SMS_SDK_APP_ID);
        sendSmsRequest.setTemplateID(TEMPLATE_ID);
        sendSmsRequest.setSign(SIGN_NAME);


        //发送的手机号
        sendSmsRequest.setPhoneNumberSet(phoneNumbers);
        //发送的内容（验证码）
        sendSmsRequest.setTemplateParamSet(templateCode);

        try {
            //发送
            final SendSmsResponse sendSmsResponse = smsClient.SendSms(sendSmsRequest);
            System.out.println("发送成功：{}" + sendSmsResponse.toString());
            return true;
        } catch (TencentCloudSDKException e) {
            System.out.println("发送失败，或者剩余短信数量不足" + e);
        }
        return false;
    }


}